package com.im.strive.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.im.strive.R;
import com.im.strive.activity.WebAcitivity;
import com.im.strive.model.beans.Results;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.List;


public class PartAdapter extends RecyclerView.Adapter<PartAdapter.PartViewHolder> {
    private Context context;
    private List<Results> part_list;


    public PartAdapter(Context context, List<Results> part_list) {
        this.context = context;
        if (part_list != null) {
            this.part_list = part_list;
        }
    }

    @Override
    public PartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_page , parent , false);

        PartViewHolder holder = new PartViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final PartViewHolder holder, final int position) {

        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration
                .createDefault(context);

        String type = part_list.get(position).getType();
        switch (type) {
            case "休息视频":
                holder.part_messege.setVisibility(View.VISIBLE);
                holder.img_part.setVisibility(View.VISIBLE);
//                holder.iv_video.setVisibility(View.VISIBLE);
//                holder.textView.setText(part_list.get(position).getDesc());
                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, WebAcitivity.class);
                        intent.putExtra("url", part_list.get(position).getUrl());
                        intent.putExtra("desc", part_list.get(position).getDesc());
                        context.startActivity(intent);
                    }
                });
                break;
            case "福利":
                holder.part_messege.setVisibility(View.GONE);
                holder.img_part.setVisibility(View.VISIBLE);
//                holder.iv_video.setVisibility(View.GONE);
                Uri uri = Uri.parse(part_list.get(position).getUrl());
                holder.img_part.setImageURI(uri);
//                holder.textView.setText("lalalalal");
//                holder.view.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent = new Intent(context, ImageActivity.class);
//                        intent.putExtra("url", part_list.get(position).getUrl());
//                        intent.putExtra("desc", part_list.get(position).getDesc());
//                        context.startActivity(intent);
//                    }
//                });
                break;
            default:
                holder.part_messege.setVisibility(View.VISIBLE);
                holder.img_part.setVisibility(View.GONE);
//                holder.iv_video.setVisibility(View.GONE);
//                holder.textView.setText(part_list.get(position).getDesc());
                holder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, WebAcitivity.class);
                        intent.putExtra("url", part_list.get(position).getUrl());
                        intent.putExtra("desc", part_list.get(position).getDesc());
                        context.startActivity(intent);
                    }
                });
                break;
        }


        holder.tv_type.setText(part_list.get(position).getType());
        holder.tv_time.setText(part_list.get(position).getCreatedAt());
        holder.tv_author.setText(part_list.get(position).getWho());
        holder.tv_content.setText(part_list.get(position).getDesc());

        String mStrImgTypeUrl = part_list.get(position).getTypeurl().getFileUrl(context);

        if(mStrImgTypeUrl.equals("")){


        }else {

            ImageLoader.getInstance().init(configuration);
            ImageLoader.getInstance().displayImage(mStrImgTypeUrl , holder.img_type);
        }







    }

    @Override
    public int getItemCount() {
        return part_list.size();

    }

    class PartViewHolder extends RecyclerView.ViewHolder {
        TextView tv_author, tv_time, tv_type , tv_content;
        RelativeLayout part_messege;
        View view;
        ImageView img_part , img_type;

        public PartViewHolder(View itemView) {
            super(itemView);

            view = itemView;
            part_messege = (RelativeLayout) itemView.findViewById(R.id.part_messege);
            tv_author = (TextView) itemView.findViewById(R.id.tv_item_home_author);
            tv_time = (TextView) itemView.findViewById(R.id.tv_item_home_time);
            tv_type = (TextView) itemView.findViewById(R.id.tv_item_home_type);
            tv_content = (TextView) itemView.findViewById(R.id.tv_item_home_content);

            img_part = (ImageView) itemView.findViewById(R.id.img_item_home_part);
            img_type = (ImageView) itemView.findViewById(R.id.img_item_home_type);

        }
    }
}
